//
//  JXCustomTabBar.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/2/26.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit

@objc protocol JXCustomTabBarDelegate :NSObjectProtocol {

    optional
    
    func JXCustomTabBarClick()
}


class JXCustomTabBar: UITabBar {
    
    weak var customDelegate :JXCustomTabBarDelegate?

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = frame.width/5
        
        customButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
        
    }
    
    func buttonDidClick (){
        // 3.调用代理
        // composeDelegate?: 表示composeDelegate属性有值才执行后面的代码
        // mainTabBarComposeClick?(): 表示代理实现这个方法就调用代理的这个方法,反之代理没有实现这个方法,就不调用
        self.customDelegate?.JXCustomTabBarClick?()
    }
    
    
    lazy var customButton : UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(imageLiteral: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(imageLiteral: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        self.addSubview(button)
        
        button.addTarget(self, action: "buttonDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()

}
